﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using RISEN;
using AMain;
using Items;

namespace Inventories
{
    public abstract class Inventory : WorldElement
    {
        private List<Item> contents;
        private double spaceUsed;
        private double spaceMax;

        /// <summary>
        /// A list of Items in the Inventory
        /// </summary>
        public List<Item> Contents { 
            get { return contents; }
        }
        /// <summary>
        /// The current amount of space Items are occupying 
        /// in the Inventory
        /// </summary>
        public double SpaceUsed { 
            get { return spaceUsed; } 
        }
        /// <summary>
        /// The maximum amount of space available for Items
        /// in the Inventory
        /// </summary>
        public double SpaceMax { 
            get { return spaceMax; }
        }
        public double Weight {
            get { return GetWeight(); }
        }

        public Inventory() : base("Inventory") 
        {
            spaceMax = Constants.SPACE_DEFUALT;
        }
        public Inventory(String inName, double inSpace) : base(inName)
        {
            spaceMax = inSpace;
        }
        public Inventory(String inName, double inSpace, List<Item> initial) : base(inName)
        {
            spaceMax = inSpace;
            AddItems(initial);
        }

        /// <summary>
        /// Add an Item to the contents of the Inventory
        /// </summary>
        /// <param name="toAdd">The Item to add</param>
        /// <returns>True if the item was successfuly added, false otherwise.</returns>
        public bool AddItem(Item toAdd)
        {
            if (CanAdd(toAdd))
            {
                contents.Add(toAdd);
                return true;
            }
            return false;
        }

        /// <summary>
        /// Adds a list of Items to the contents of the Inventory
        /// and returns the list of Items that weren't added
        /// </summary>
        /// <param name="toAdd">Items to add</param>
        /// <returns>The list of Items that weren't added</returns>
        public List<Item> AddItems(List<Item> toAdd)
        {
            while(toAdd.Count != 0 && CanAdd(toAdd.First()))
            {
                AddItem(toAdd.First());
                toAdd.RemoveAt(0);
            }
            return toAdd;
        }

        public double GetRemainingSpace()
        {
            return spaceMax - spaceUsed;
        }

        private bool CanAdd(Item item)
        {
            if (item.Size + spaceUsed <= spaceMax)
            {
                return true;
            }
            return false;
        }

        private double GetWeight()
        {
            double ret = 0;
            for (int i = 0; i < contents.Count; i++)
            {
                ret += contents.ElementAt(i).Weight;
            }
            return ret;
        }

    }
}
